package com.lqr.wechat.activity;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.bridge.imconfig.NimAccountSDK;
import com.lqr.wechat.R;
import com.lqr.wechat.databinding.ActivitySplashBinding;
import com.lqr.wechat.rxpremissions.PermissionsUtils;
import io.reactivex.functions.Consumer;

/**
 * @创建者 CSDN_LQR
 * @描述 欢迎界面
 */
public class SplashActivity extends BaseActivity {

    private ActivitySplashBinding binding ;
    @Override
    public void initListener() {
        binding.btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PermissionsUtils.checkPermission(SplashActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE, new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if(aBoolean){
                            //true  表示是第一次进入   ，false表示不是第一次
                            Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            finish();

//                    startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                        }else {
                            PermissionsUtils.showPermissionDialog(SplashActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE,
                                    "您可以在“手机管家”>“权限管理”中配置权限", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            SplashActivity.this.finish();
                                        }
                                    });
                        }
                    }
                });
            }
        });
        binding.btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PermissionsUtils.checkPermission(SplashActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE, new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if(aBoolean){
                            //true  表示是第一次进入   ，false表示不是第一次
//                    startActivity(new Intent(this, RegisterActivity.class));
                        }else {
                            PermissionsUtils.showPermissionDialog(SplashActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE,
                                    "您可以在“手机管家”>“权限管理”中配置权限", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            SplashActivity.this.finish();
                                        }
                                    });
                        }
                    }
                });
            }
        });
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_splash;
    }

    @Override
    public void init() {
        if (canAutoLogin()) {
            //登录到主界面
            startActivity(new Intent(this, MainActivity.class));
            finish();
        }
    }

    @Override
    public void initView() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏标题栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//隐藏状态栏
        binding = getBind() ;
    }

    /**
     * 判断是否可以自动登录
     *
     * @return
     */
    public boolean canAutoLogin() {
        String account = NimAccountSDK.getUserAccount();
        String token = NimAccountSDK.getUserAccount();
        return !TextUtils.isEmpty(account) && !TextUtils.isEmpty(token);
    }

}
